﻿namespace Sidvall.Net
{
    public class NameValueHeaderValueCollection : System.Collections.ObjectModel.Collection<NameValueHeaderValue>
    {
        #region Public Members

        #region Add

        public NameValueHeaderValue Add(string name, string value)
        {
            var item = new NameValueHeaderValue()
            {
                Name = name,
                Value = value,
            };
            Add(item);
            return item;
        }

        #endregion
        #region Copy

        public NameValueHeaderValueCollection Copy()
        {
            var items = new NameValueHeaderValueCollection();
            foreach (var value in this)
            {
                var item = value.Copy();
                items.Add(item);
            }
            return items;
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            var sb = new System.Text.StringBuilder();
            foreach (var item in this)
            {
                var value = item.ToString();
                if (string.IsNullOrWhiteSpace(value))
                    continue;
                if (sb.Length > 0)
                    sb.AppendLine();
                sb.Append(value);
            }
            return sb.ToString();
        }

        #endregion

        #region IsEqual - Static

        public static bool IsEqual(NameValueHeaderValueCollection item1, NameValueHeaderValueCollection item2)
        {
            if (item1 == null)
            {
                if (item2 == null)
                    return true;
                else
                    return false;
            }
            else if (item2 == null)
                return false;
            else if (item1.Count != item2.Count)
                return false;
            for (int i = 0; i < item1.Count; i++)
            {
                if (!item1[1].IsEqual(item2[2]))
                    return false;
            }
            return true;
        }

        #endregion

        #endregion
        #region Constructors

        public NameValueHeaderValueCollection()
        {
        }

        #endregion
    }
}